$(document).ready(function () {
    var userId = $('#userId').val();
    $(".face_upload").each(function () {

        var  this_ = $(this);

        // 获取选择图片的盒子
        var upload_btn =    this_.find(".upload_btn");
        var  upload_btn_id =  upload_btn.attr("id");

        // 获取装图片的盒子
        var upload_img =    this_.find(".upload_img");
        var  upload_img_id =  upload_img.attr("id");

        // 获取keyName值
        var keyName = this_.find('.file_input_value');
        var keyName_val = keyName.attr('keyName');

        var img_value = this_.find('.file_input_value').val();
        if (img_value != null && img_value != "" && img_value != undefined) {
            var img_url = "/wxapply/getMedia?mediaId=" + img_value;
            var img = this_.find(".upload_img img");
            img.attr('src', img_url);//图片链接（base64
        }
        var id = $('#wxMchId').val();
        $.fileUpload({
            filebutton: "#"+upload_btn_id,
            previewZoom: "#"+upload_img_id,
            keyName_val: keyName_val,
            httpUrl: "/wXmchUpgrade/wXmchUpgrade",
            userId: userId,
            id: id
        });

    });

    // 主体开始
    $('.subject').on('click',function () {
        $('.subject ul').toggle();
    });
    $('.subject li').on('click',function () {
        var subjectVal = $('.subject li').eq($(this).index()).text();
        var liVal = $('.subject li').eq($(this).index()).attr('value');
        $('.subject span').text(subjectVal);
        $('.subject input').val(liVal);
    });
    // 主体结束

    // 经营场景开始
    $('.scene').on('click',function () {
        $('.scene ul').toggle();
    });
    $('.scene li').on('click',function () {
        var sceneVal = $('.scene li').eq($(this).index()).text();
        var liVal = $('.scene li').eq($(this).index()).attr('value');
        $('.scene span').text(sceneVal);
        $('.scene input').val(liVal);
    });
    // 经营场景结束

    // 营业执照类型开始
    $('.B_license').on('click',function () {
        $('.B_license ul').toggle();
    });
    $('.B_license li').on('click',function () {
        var licenseVal = $('.B_license li').eq($(this).index()).text();
        var liVal = $('.B_license li').eq($(this).index()).attr('value');
        $('.B_license span').text(licenseVal);
        $('.B_license input').val(liVal);
    });
    // 营业执照类型结束

    // 提交进件
    $('.preservation').on('click',function () {
        var formData = $('form').serializeArray();
        // 经营校验
        var organization_type = $('input[name=organization_type]').val();
        var business_license_copy =  $('#bu_img img').attr("src");
        var business_license_number = $('input[name=business_license_number]').val();
        var merchant_name = $('input[name=merchant_name]').val();

        var company_address = $('input[name=company_address]').val();
        var legal_person = $('input[name=legal_person]').val();
        var business_time = $('input[name=business_time]').val();
        var business_licence_type = $('input[name=business_licence_type]').val();

        var merchant_shortname = $('input[name=merchant_shortname]').val();
        var business = $('input[name=business]').val();
        var business_scene = $('input[name=business_scene]').val();


        if(!organization_type){
            $.toast("主体类型必选");
        }else if(!business_license_copy){
            $.toast("营业执照扫描件必传");
        }else if(!business_license_number){
            $.toast("营业执照注册号必填");
        }else if(!merchant_name){
            $.toast("商户名称必填");
        }else if(!company_address){
            $.toast("注册地址必填");
        }else if(!legal_person){
            $.toast("经营者姓名或法定代表人必填");
        }else if(!(/^\d{4}-\d{2}-\d{2}[^|]*?[|]?\d{4}-\d{2}-\d{2}$/.test(business_time))){
            $.toast("营业期限有误");
        }else if(!business_licence_type){
            $.toast("营业执照类型必选");
        }else if(!merchant_shortname){
            $.toast("商户简称必填");
        }else if(!business){
            $.toast("费率结算规则ID必填");
        }else if(!business_scene){
            $.toast("经营场景必选");
        }else{
            $.post('/wXmchUpgrade/subinfo',formData,function (result) {
                if(result.code==1){
                    $.toast("提交进件成功");
                }else{
                    $.toast(result.message);
                }
            });
        }
    });

    // 暂存开始
    $('.temporary_storage').on('click',function () {
        var formData = $('form').serializeArray();
        $.post('/wXmchUpgrade/edit',formData,function (result) {
            if(result.code==1){
                $.toast("暂存成功");
            }else{
                $.toast(result.message);
            }
        });
    });

    // 三个选择按钮回显赋值
    var organization_type = $('input[name=organization_type]').val();
    var business_licence_type = $('input[name=business_licence_type]').val();
    var business_scene = $('input[name=business_scene]').val();
    if(organization_type=='2'){
        $('.subject span').text('企业')
    }else if(organization_type=='4'){
        $('.subject span').text('个体工商户')
    }else if(organization_type=='3'){
        $('.subject span').text('党政、机关及事业单位')
    }else if(organization_type=='1708'){
        $('.subject span').text('其他组织')
    }


    if(business_licence_type=='1762'){
        $('.B_license span').text('已三证合一')
    }else if(business_licence_type=='1763'){
        $('.B_license span').text('未三证合一')
    }


    if(business_scene=='[1721]'){
        $('.scene span').text('线下')
    }else if(business_scene=='[1837]'){
        $('.scene span').text('公众号')
    }else if(business_scene=='[1838]'){
        $('.scene span').text('小程序')
    }else if(business_scene=='[1840]'){
        $('.scene span').text('PC网站')
    }else if(business_scene=='[1724]'){
        $('.scene span').text('APP')
    }

});